﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Arch.Samples.RiaServices.Commands;
using Arch.Samples.Model;
using Arch.Samples.RiaServices.Services;
using System.ServiceModel.DomainServices.Client;

namespace Arch.Samples.RiaServices.Infrastructure
{
    public class CollectionViewModelBase<T> : ContextViewModelBase where  T : Entity
    {
        public ICollection<T> Items { get; set; }
        public ICollection<T> SelectedItems { get; set; }

        public CollectionViewSource CollectionViewSource { get; set; }

        public ICommand AddCommand { get; protected set; }
        public ICommand EditCommand { get; protected set; }
        public ICommand DeleteCommand { get; protected set; }
        public ICommand DeleteSelectedCommand { get; protected set; }

        public Todo CurrentItem
        {
            get
            {
                return CollectionViewSource.View.CurrentItem as Todo;
            }
            set
            {
                CollectionViewSource.View.MoveCurrentTo(value);
            }
        }

        public CollectionViewModelBase(TodoContext context) : base(context)
        {
            Items = new ObservableCollection<T>();
            SelectedItems = new ObservableCollection<T>();
            CollectionViewSource = new CollectionViewSource() { Source = Items };

            CollectionViewSource.View.CurrentChanged += new EventHandler(CurrentChanged);
            CollectionViewSource.View.CurrentChanging += new CurrentChangingEventHandler(CurrentChanging);

            SubmitCommand = new SubmitCollectionCommand<CollectionViewModelBase<T>, T>(this);
        }

        protected virtual void CurrentChanging(object sender, CurrentChangingEventArgs e)
        {
        }

        protected virtual void CurrentChanged(object sender, EventArgs e)
        {
            RaisePropertyChanged("CurrentItem");
        }
    }
}
